package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.dto.e.DataAnalysisJudgeEntDTO;
import com.macro.mall.dto.o.DataAnalysisJudgeOutDTO;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.service.DataAnalysisJudgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataAnalysisJudgeServiceImpl implements DataAnalysisJudgeService {

    @Autowired
    private DpsJudgeMapper dpsJudgeMapper;
    @Autowired
    private BasicKeyTextMapper basicKeyTextMapper;
    @Autowired
    private BasicTopicMapper basicTopicMapper;
    @Autowired
    private BasicKeyImgMapper basicKeyImgMapper;

    @Override
    public PageInfo<DataAnalysisJudgeOutDTO> list(DataAnalysisJudgeEntDTO dataAnalysisJudgeEntDTO) {
        PageHelper.startPage(dataAnalysisJudgeEntDTO.getPage(),dataAnalysisJudgeEntDTO.getPageSize());
        List<DpsJudge> list = dpsJudgeMapper.list(dataAnalysisJudgeEntDTO.getKeyName());
        PageInfo<DpsJudge> dpsJudgePageInfo = new PageInfo<>(list);
        List<DataAnalysisJudgeOutDTO> list1=new ArrayList<>();
        for (DpsJudge dpsJudge:list){
            DataAnalysisJudgeOutDTO dataAnalysisJudgeOutDTO = new DataAnalysisJudgeOutDTO();
            BasicKeyText basicKeyText = basicKeyTextMapper.selectByPrimaryKey(dpsJudge.getContentId());
            BasicTopic basicTopic = basicTopicMapper.selectByPrimaryKey(dpsJudge.getContentId());
            BasicKeyImg basicKeyImg = basicKeyImgMapper.selectByPrimaryKey(dpsJudge.getContentId());
            if (basicKeyText !=null){
                dataAnalysisJudgeOutDTO.setName(basicKeyText.getKeyText());
            }
            if (basicTopic !=null){
                dataAnalysisJudgeOutDTO.setName(basicTopic.getTopicName());
            }
            if (basicKeyImg !=null){
                dataAnalysisJudgeOutDTO.setName(basicKeyImg.getImgName());
                dataAnalysisJudgeOutDTO.setPath(basicKeyImg.getImgPath());
            }
            dataAnalysisJudgeOutDTO.setId(dpsJudge.getId());
            dataAnalysisJudgeOutDTO.setCreatedDt(dpsJudge.getCreatedDt());
            dataAnalysisJudgeOutDTO.setStatus(dpsJudge.getStatus());
            dataAnalysisJudgeOutDTO.setLabel(dpsJudge.getJudgeResultType());
            dataAnalysisJudgeOutDTO.setTargetType(dpsJudge.getJudgeTargetType());
            list1.add(dataAnalysisJudgeOutDTO);
        }
        PageInfo<DataAnalysisJudgeOutDTO> dataAnalysisJudgeOutDTOPageInfo = new PageInfo<>(list1);
        dataAnalysisJudgeOutDTOPageInfo.setTotal(dpsJudgePageInfo.getTotal());
        return dataAnalysisJudgeOutDTOPageInfo;
    }

    @Override
    public int modify(DataAnalysisJudgeEntDTO dataAnalysisJudgeEntDTO) {
        DpsJudge dpsJudge1 = dpsJudgeMapper.selectByPrimaryKey(dataAnalysisJudgeEntDTO.getId());
        if (dpsJudge1 ==null){
            return 0;
        }
        BasicKeyText basicKeyText = basicKeyTextMapper.selectByPrimaryKey(dpsJudge1.getContentId());
        BasicTopic basicTopic = basicTopicMapper.selectByPrimaryKey(dpsJudge1.getContentId());
        BasicKeyImg basicKeyImg = basicKeyImgMapper.selectByPrimaryKey(dpsJudge1.getContentId());
        if (basicKeyText !=null){
            basicKeyText.setKeyText(dataAnalysisJudgeEntDTO.getKeyName());
            basicKeyTextMapper.updateByPrimaryKeySelective(basicKeyText);

        }
        if (basicTopic !=null){
            basicTopic.setTopicName(dataAnalysisJudgeEntDTO.getKeyName());
            basicTopicMapper.updateByPrimaryKeySelective(basicTopic);
        }
        if (basicKeyImg !=null){
            basicKeyImg.setImgName(dataAnalysisJudgeEntDTO.getKeyName());
            basicKeyImgMapper.updateByPrimaryKeySelective(basicKeyImg);
        }

        DpsJudge dpsJudge = new DpsJudge();
        dpsJudge.setId(dataAnalysisJudgeEntDTO.getId());
        dpsJudge.setStatus(dataAnalysisJudgeEntDTO.getStatus());
        dpsJudge.setJudgeTargetType(dataAnalysisJudgeEntDTO.getLabel());
        dpsJudge.setJudgeTargetType(dataAnalysisJudgeEntDTO.getResultType());
        return dpsJudgeMapper.updateByPrimaryKeySelective(dpsJudge);
    }
}
